package com.snow.useviewdemo.function2.viewfliper;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.snow.useviewdemo.R;

import java.util.List;

/**
 * Created by zhaoyingtao
 * Date: 2020/6/16
 * Describe:
 */
public class UPMarqueeView extends ViewFlipper {
    private Context mContext;
//    private int interval = 2000;
    /**
     * 动画时间
     */
    private int animDuration = 1000;

    public UPMarqueeView(Context context) {
        super(context);
        init(context, null, 0);
    }

    public UPMarqueeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        this.mContext = context;
//        setFlipInterval(interval);
        Animation animIn = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_in);
        animIn.setDuration(animDuration);
        setInAnimation(animIn);
        Animation animOut = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_out);
        animOut.setDuration(animDuration);
        setOutAnimation(animOut);
    }

    /**
     * 设置循环滚动的View数组
     *
     * @param views
     */
    public void setViews(List<?> views) {
        if (views == null || views.size() == 0) return;
        removeAllViews();
        for (int i = 0; i < views.size(); i++) {
            Object T = views.get(i);
            if (T instanceof TextView) {
                addView((TextView) views.get(i));
            } else if (T instanceof ImageView) {
                addView((ImageView) views.get(i));
            } else if (T instanceof View) {
                addView((View) views.get(i));
            }
        }
        startFlipping();
    }

    /**
     * 当前界面退出也停止动画
     */
    public void stopAnimation() {
        stopFlipping();
        removeAllViews();
    }
}